package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.mvp.present.Cmd11Presenter;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 21校时
 * Created by zwz on 2020/6/22.
 */
public class Cmd_21Fragment extends BaseFragment {


    @BindView(R.id.tv_time)
    EditText tvTime;
    @BindView(R.id.cb_auto)
    CheckBox cbAuto;
    @BindView(R.id.tv_timeinfo)
    TextView tvTimeinfo;

    private String data;
    private String dataChoose;


    public static Cmd_21Fragment newInstance() {
        Bundle args = new Bundle();
        Cmd_21Fragment fragment = new Cmd_21Fragment();
        fragment.setArguments(args);


        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_21;
    }

    @Override
    protected void initView() {
        Date date = new Date(System.currentTimeMillis());
        dataChoose = new SimpleDateFormat("yy MM dd HH mm ss", Locale.CHINA).format(date);
        tvTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(date));
    }

    /**
     * 设置终端时间
     */
    private void sendData() {
        if (cbAuto.isChecked()) {
            data = new SimpleDateFormat("yy MM dd HH mm ss", Locale.CHINA).format(new Date(System.currentTimeMillis()));
        } else {
            data = dataChoose;
        }

        String[] str = data.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : str) {
            stringBuilder.append(String.format("%02x", Integer.parseInt(s))).append(" ");
        }

        String cmd = CmdUtils.getCmd(CmdConfig.CMD_21, stringBuilder.substring(0, stringBuilder.length() - 1));

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();

            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });

    }

    @OnClick({R.id.tv_time, R.id.btn_get, R.id.btn_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_time:
                showDialog();
                break;
            case R.id.btn_get:
                getInfo(tvTimeinfo);
                break;
            case R.id.btn_set:
                sendData();
                break;
        }
    }

    /**
     * 获取终端时间
     */
    private void getInfo(TextView tv) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_11);
        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                //回调:EE EE 30 30 30 30 30 30 30 30 13 00 11 14 07 07 0A 1C 32 48 86 FF FF
                dismissLoadingDialog();
                Cmd11Presenter presenter = new Cmd11Presenter();
                CommonBean<String> receiveData = presenter.paseData(msg);
                toastShow(receiveData.getData());
                tv.setText(String.format("终端时间：%s", receiveData.getData()));
            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }

    /**
     * 时间选择
     */
    private void showDialog() {
//        DatePickDialog dialog = new DatePickDialog(_mActivity);
//        //设置上下年分限制
//        dialog.setYearLimt(5);
//        //设置标题
//        dialog.setTitle("选择时间");
//        //设置类型
//        dialog.setType(DateType.TYPE_ALL);
//        //设置消息体的显示格式，日期格式
//        dialog.setMessageFormat("yyyy-MM-dd HH:mm:ss");
//        //设置选择回调
//        dialog.setOnChangeLisener(null);
//        //设置点击确定按钮回调
//        dialog.setOnSureLisener(new OnSureLisener() {
//            @SuppressLint("SimpleDateFormat")
//            @Override
//            public void onSure(Date date) {
//                //                tvTime.setText(dateToString(date,"yyyy-MM-dd HH:mm:ss"));
//                dataChoose = new SimpleDateFormat("yy MM dd HH mm ss", Locale.CHINA).format(date);
//                tvTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(date));
//            }
//        });
//        dialog.show();

        TimePickerView pvTime = new TimePickerBuilder(_mActivity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                dataChoose = new SimpleDateFormat("yy MM dd HH mm ss", Locale.CHINA).format(date);
                tvTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(date));
            }
        })   .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式

                .build();
        pvTime.show();
    }


}
